import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;

public class MathPracticeNoRepeat {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); //获取用户输入
        Random random = new Random(); //创建随机数生成器
        int num1,num2,result,answer;
        char operation; // 加减运算符
        Set<String> generatedQuestions = new HashSet<>();

        System.out.println("加减法口算练习系统-v0.1");
        System.out.println("请回答以下问题：");

        while (generatedQuestions.size() < 50) {
            // 生成两个随机数和一个随机操作符
            num1 = random.nextInt(100);
            num2 = random.nextInt(100);
            operation = random.nextBoolean() ? '+' : '-';

            // 确保减法操作不会产生负数
            if (operation == '-' && num1 < num2) {
                int temp = num1;
                num1 = num2;
                num2 = temp;
            }

            // 创建题目字符串
            String question = String.format("%d %c %d", num1, operation, num2);

            // 检查是否已经生成过这个题目
            if (!generatedQuestions.contains(question)) {
                generatedQuestions.add(question);

                // 打印题目
                System.out.print(question + " = ");

                // 用户输入答案
                answer = scanner.nextInt();

                // 计算结果
                switch (operation) {
                    case '+':
                        result = num1 + num2;
                        break;
                    case '-':
                        result = num1 - num2;
                        break;
                    default:
                        result = 0; // 不应该发生
                }

                // 检查答案是否正确
                if (result == answer) {
                    System.out.println("正确！");
                } else {
                    System.out.printf("错误！正确答案是：%d\n", result);
                }
            }
        }

        scanner.close();
        System.out.println("练习结束，感谢使用加减法口算练习系统！");
    }
}
